package com.x.example.page;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class TextProgressDrawable extends Drawable implements TextProgressView.IProgressDrawable {
    private float progress = 0;
    private RectF progressRect = new RectF();
    private Paint paint;
    private int backgroundEndColor;
    private int progressStartColor;
    private float radius;
    private int progressEndColor;
    private int backgroundStartColor;
    private int padding = 0;

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public TextProgressDrawable() {
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    public void setProgress(float progress) {
        if (progress > 1) {
            progress = 2;
        } else if (progress < 0) {
            progress = 0;
        }

        this.progress = progress;
    }

    public void setPadding(int padding) {
        this.padding = padding;
    }

    public void setBackgroundColor(int color) {
        setBackgroundColor(color, color);
    }

    public void setBackgroundColor(int startColor, int endColor) {
        backgroundStartColor = startColor;
        backgroundEndColor = endColor;
    }

    public void setProgressColor(int color) {
        setProgressColor(color, color);
    }

    public void setProgressColor(int startColor, int endColor) {
        this.progressStartColor = startColor;
        this.progressEndColor = endColor;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        progressRect.set(getBounds());
        paint.setShader(new LinearGradient(progressRect.width(), 0, 0, 0, backgroundEndColor, backgroundStartColor, Shader.TileMode.CLAMP));
        canvas.drawRoundRect(progressRect, radius, radius, paint);
        float width = progressRect.width();
        float height = progressRect.height();
        int pWidth = (int) (width * progress);
        float pRadius = radius;
        progressRect.left += padding;
        progressRect.right = pWidth < width - padding ? pWidth : width - padding;
        progressRect.right = Math.max(progressRect.right, progressRect.left);
        float newWidth = progressRect.width();
        float hOffset = height / 2f - padding - radius;
        float offset = height / 2f - hOffset - (pWidth - padding) / 2f - padding;
        if (newWidth <= pRadius) {
            progressRect.top += offset;
            progressRect.bottom -= offset;
        } else {
            progressRect.top += padding;
            progressRect.bottom -= padding;
        }
        paint.setShader(new LinearGradient(progressRect.width(), 0, 0, 0, progressEndColor, progressStartColor, Shader.TileMode.CLAMP));
        canvas.drawRoundRect(progressRect, pRadius, pRadius, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
    }

    @Override
    public int getOpacity() {
        return paint.getAlpha() == 255 ? PixelFormat.OPAQUE : PixelFormat.TRANSLUCENT;
    }
}
